package cn.interestingshop.web.client;

import cn.interestingshop.entity.Goods;
import cn.interestingshop.entity.User;
import cn.interestingshop.service.classify.ClassifyService;
import cn.interestingshop.service.classify.ClassifyServiceImpl;
import cn.interestingshop.service.goods.GoodsService;
import cn.interestingshop.service.goods.GoodsServiceImpl;
import cn.interestingshop.utils.ClassifyVo;
import cn.interestingshop.utils.EmptyUtils;
import cn.interestingshop.utils.MemcachedUtils;
import cn.interestingshop.utils.Pager;
import cn.interestingshop.web.AbstractServlet;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "GoodsServlet",urlPatterns = {"/goods","/favorite"})
public class GoodsServlet extends AbstractServlet {



    @Override
    public Class getServletClass() {
        return GoodsServlet.class;
    }



    public String queryGoodsList(HttpServletRequest request, HttpServletResponse response){

        String currentPage = request.getParameter("currentPage");
        int currentPageNo = EmptyUtils.isEmpty(currentPage)?1:Integer.valueOf(currentPage);

        ClassifyService classifyService=new ClassifyServiceImpl();
        GoodsService goodsService=new GoodsServiceImpl();

        List<Goods> goodsList=new ArrayList<>();

        goodsList=goodsService.selectOne(Integer.valueOf(request.getParameter("category")),Integer.valueOf(request.getParameter("level")),currentPageNo,8);
        int total=goodsService.findUserCount(Integer.valueOf(request.getParameter("category")),Integer.valueOf(request.getParameter("level")));

        List<ClassifyVo> list = classifyService.selectAll();

        int count = goodsService.findUserCount(Integer.valueOf(request.getParameter("category")),Integer.valueOf(request.getParameter("level")));
        Pager pager = new Pager(count,8,currentPageNo);
        pager.setUrl("goods?action=queryGoodsList&level="+request.getParameter("level")+"&category="+request.getParameter("category"));

        request.setAttribute("classifyVoList",list);

        request.setAttribute("goodsList",goodsList);
        request.setAttribute("pager", pager);
        request.setAttribute("total",total);

        return "/client/goods/queryGoodsList";
    }

    public String queryGoodsDetail(HttpServletRequest request,HttpServletResponse response){
        GoodsService goodsService=new GoodsServiceImpl();



        Goods goods = goodsService.selectOne(Integer.valueOf(request.getParameter("id")));

        ClassifyService classifyService=new ClassifyServiceImpl();

        List<ClassifyVo> list = classifyService.selectAll();

        request.setAttribute("classifyVoList",list);

        request.setAttribute("goods",goods);

        return "/client/goods/goodsDetail";
    }


    public String queryGoodsListSs(HttpServletRequest request,HttpServletResponse response){




        String currentPage = request.getParameter("currentPage");
        int currentPageNo = EmptyUtils.isEmpty(currentPage)?1:Integer.valueOf(currentPage);


        GoodsService goodsService=new GoodsServiceImpl();
        ClassifyService classifyService=new ClassifyServiceImpl();

        List<Goods> goods = goodsService.selectAll(request.getParameter("keyWord"));

        int count = goodsService.findUserCount(goods);

        goods=goodsService.handlePageList(goods,currentPageNo,8);


        Pager pager = new Pager(count,8,currentPageNo);
        pager.setUrl("goods?action=queryGoodsListSs&keyWord="+request.getParameter("keyWord"));

        List<ClassifyVo> list = classifyService.selectAll();

        request.setAttribute("goodsList",goods);
        request.setAttribute("keyWord",request.getParameter("keyWord"));
        request.setAttribute("classifyVoList",list);
        request.setAttribute("pager", pager);
        request.setAttribute("total",count);

        return "/client/goods/queryGoodsList";
    }


    public void addFavorite(HttpServletRequest request,HttpServletResponse response){

        HttpSession httpSession=request.getSession();


        List<Goods> goodsList=null;

        if(EmptyUtils.isEmpty(httpSession.getAttribute("recentGoods"))){
            goodsList=new ArrayList<>();
        }else{
            goodsList= (List<Goods>) httpSession.getAttribute("recentGoods");
            if(goodsList.size()==5){
                goodsList.remove(0);
            }
        }




        Goods goods=new Goods();



        int goodsId=0;
        if(!EmptyUtils.isEmpty(request.getParameter("id"))){
            goodsId= Integer.parseInt(request.getParameter("id"));
        }

        for (Goods goods1 : goodsList) {
            if(goods1.getId()==goodsId){
                goodsId=-1;
                break;
            }
        }

        JSONObject jsonObject=new JSONObject();

        if(goodsId!=-1){



        GoodsService goodsService = new GoodsServiceImpl();

        goods = goodsService.selectOne(goodsId);

        goodsList.add(goods);


        httpSession.setAttribute("recentGoods", goodsList);




        jsonObject.put("status",1);

        }else{
            jsonObject.put("status",0);
            jsonObject.put("message","收藏重复");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void toFavoriteList(HttpServletRequest request,HttpServletResponse response){



    }



}
